package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.common.service.AttachmentService;
import com.koron.common.core.business.workflow.bean.po.SysWorkflow;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpBudgetReview;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.query.SpEstablishQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpEstablishVO;
import com.koron.sinoprobe.web.engineering.mapper.SpEstablishMapper;
import com.koron.sinoprobe.web.engineering.service.SpBudgetReviewService;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 项目立项(SpEstablish)表服务实现类
 *
 * @author jzp
 * @since 2021-11-30 10:06:50
 */
@Service
public class SpEstablishServiceImpl implements SpEstablishService {

    @Autowired
    private SysWorkflowService sysWorkflowService;

    @Autowired
    private SpBudgetReviewService spBudgetReviewService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpEstablishVO getById(SessionFactory factory, String id) {
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        SpEstablishVO spEstablishVO = mapper.selectById(id);
        SpProject spProject = spProjectService.getById(factory, spEstablishVO.getProjectId());
        spEstablishVO.setSpProject(spProject);
        return spEstablishVO;
    }

    @Override
    @TaskAnnotation("getByProjectId")
    public SpEstablish getByProjectId(SessionFactory factory, String projectId) {
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.selectOneByProjectId(projectId);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpEstablishVO> list(SessionFactory factory, SpEstablishQuery query) {
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpEstablishVO> page(SessionFactory factory, PageQuery pageQuery, SpEstablishQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpEstablishVO> spEstablishList = list(factory, query);
        return new PageInfo<>(spEstablishList);
    }

    @Override
    @TaskAnnotation("getRelated")
    public List<String> getRelated(SessionFactory factory) {
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.selectRelated();
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpEstablish spEstablish) {
        // 默认草稿
        if (StringUtils.isEmpty(spEstablish.getStatus())) {
            spEstablish.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.insertOrUpdate(spEstablish) > 0;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        SpEstablishVO source = getById(factory, id);
        // 1. 更新状态
        SpEstablish spEstablish = new SpEstablish();
        spEstablish.setId(id);
        spEstablish.setStatus(Constants.AppceptStatus.APPROVED);
        spEstablish.setFinishTime(new Date());
        update(factory, spEstablish);

        // 2. 如果同时预算审查，审批完成后自动生成一个已完成的预算审查单
        if (new Integer(1).equals(source.getIsSkipBudgetCheck())) {
            // 2.1 自动生成一个已完成的预算审查单
            SpBudgetReview spBudgetReview = new SpBudgetReview();
            spBudgetReview.setProjectId(source.getProjectId());
            spBudgetReview.setTitle(Objects.toString(source.getProjectName(), "") + "_预算审查");
            spBudgetReview.setBudget(source.getBudget());
            spBudgetReview.setBudgetNum(source.getBudgetNum());
            spBudgetReview.setBudgetPrice(source.getBudgetPrice());
            spBudgetReview.setBudgetAuditPrice(source.getBudgetAuditPrice());
            spBudgetReview.setPredictStartTime(source.getPredictStartTime());
            spBudgetReview.setPredictEndTime(source.getPredictEndTime());
            spBudgetReview.setAuthorizer(source.getLeader());
            spBudgetReview.setAuthorizerName(source.getLeaderName());
            spBudgetReview.setAuditor(source.getAuditor());
            spBudgetReview.setAuditorName(source.getAuditorName());
            spBudgetReview.setAuditFinishTime(new Date());
            spBudgetReview.setStatus(Constants.AppceptStatus.APPROVED);
            spBudgetReview.setFileType(source.getBudgetFileType());
            spBudgetReviewService.saveOrUpdate(factory, spBudgetReview);

            // 2.2 复制工作流
            SysWorkflow sysWorkflow = sysWorkflowService.queryOneByDataId(id);
            sysWorkflow.setDataId(spBudgetReview.getId());
            sysWorkflow.setModuleCode(null);
            sysWorkflowService.insert(sysWorkflow);

            // 2.3 复制附件
            attachmentService.copyAttachmentWithType(id, "budgetFiles", spBudgetReview.getId());
        }

        // 立项完成后，项目的状态改为规划中，进度为1%
        SpProjectVO spProjectVO = spProjectService.selectOne(factory, source.getProjectId());
        spProjectVO.setMajor(source.getMajorCategory());
        if (spProjectVO.getProgress() > 0) {
            return true;
        }
        spProjectVO.setProgress(1);
        spProjectService.saveOrUpdate(factory, spProjectVO);
        return true;
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpEstablish spEstablish) {
        spEstablish.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
        saveOrUpdate(factory, spEstablish);
        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpEstablish spEstablish) {
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.update(spEstablish) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpEstablish spEstablish = new SpEstablish();
        spEstablish.setId(id);
        spEstablish.setStatus(status);
        return update(factory, spEstablish);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpEstablish source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpEstablishMapper mapper = factory.getMapper(SpEstablishMapper.class);
        return mapper.hidden(id) > 0;
    }
}
